<?php
namespace app\agent\controller;

use app\admin\widget\Name;
use app\common\cache\bank\IBankCache;
use app\common\cache\bank\IBankTypeCache;
use app\common\cache\common\IAreaCache;
use app\common\cache\credit\ICreditCache;
use app\common\cache\credit\ICreditFavoriteCache;
use app\common\cache\credit\ICreditRecommendCache;
use app\common\cache\credit\ICreditTagCache;
use app\common\controller\AgentBaseController;
use app\common\enum\CreditStatus;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\ILog;
use app\common\ext\IRequest;
use think\Db;

class Credit extends AgentBaseController{

    /**
     * @功能：已上架信用卡列表
     * @开发者：cxl
     * @return string
     */
    public function credit_up_list(){
        //接收参数
        $bank_id = IRequest::get('bank_id');
        $credit_product_id = IRequest::get('credit_product_id');
        $credit_bank_code = IRequest::get('credit_bank_code');
        $credit_product_name = IRequest::get('credit_product_name');
        $credit_status = IRequest::get('credit_status');
        $credit_fee = IRequest::get('credit_fee');
        $credit_level = IRequest::get('credit_level');
        $credit_currency = IRequest::get('credit_currency');
        $credit_agency = IRequest::get('credit_agency');
        $credit_privilege = IRequest::get('credit_privilege');
        $credit_purpose = IRequest::get('credit_purpose');
        $credit_color = IRequest::get('credit_color');
        $credit_type = IRequest::get('credit_type');

        //设置查询条件
        $credit_product_where['agent_id'] = get_login_agent_id();
        $credit_product_where['is_delete'] = IsDelete::No;
        $credit_product_where['credit_status'] = CreditStatus::release;
        if(!empty($bank_id)) $credit_product_where['bank_id'] = $bank_id;
        if(!empty($credit_product_id)) $credit_product_where['credit_product_id'] = $credit_product_id;
        if(!empty($credit_bank_code)) $credit_product_where['credit_bank_code'] = array("like","%{$credit_bank_code}%");
        if(!empty($credit_product_name)) $credit_product_where['credit_product_name'] = array("like","%{$credit_product_name}%");
        if(!empty($credit_status)) $credit_product_where['credit_status'] = $credit_status;
        if(!empty($credit_fee)) $credit_product_where['credit_fee'] = $credit_fee;
        if(!empty($credit_level)) $credit_product_where['credit_level'] = $credit_level;
        if(!empty($credit_currency)) $credit_product_where[''] =  array("exp","find_in_set({$credit_currency}, credit_currency)");
        if(!empty($credit_agency)) $credit_product_where['credit_agency'] = $credit_agency;
        if(!empty($credit_privilege)) $credit_product_where['credit_privilege'] = $credit_privilege;
        if(!empty($credit_purpose)) $credit_product_where['credit_purpose'] = $credit_purpose;
        if(!empty($credit_color)) $credit_product_where['credit_color'] = $credit_color;
        if(!empty($credit_type)) $credit_product_where['credit_type'] = $credit_type;

        //取得管理员详情
        $credit_list = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbOrder("credit_product_id desc")->pag();
        if ($credit_list === false){
            error('获取信用卡列表失败');
        }


        //取得银行数据
        $bank_list = IBankCache::getList();

        //页面赋值
        $this->assign("bank_id",$bank_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_bank_code",$credit_bank_code);
        $this->assign("credit_product_name",$credit_product_name);
        $this->assign("credit_status",$credit_status);
        $this->assign("credit_fee",$credit_fee);
        $this->assign("credit_level",$credit_level);
        $this->assign("credit_currency",$credit_currency);
        $this->assign("credit_agency",$credit_agency);
        $this->assign("credit_privilege",$credit_privilege);
        $this->assign("credit_purpose",$credit_purpose);
        $this->assign("credit_color",$credit_color);
        $this->assign("credit_type",$credit_type);
        $this->assign("bank_list",$bank_list);
        $this->assign("credit_list",$credit_list);

        //渲染模板输出
        return view("credit_up_list");
    }

    /**
     * @功能：编辑中信用卡列表
     * @开发者：cxl
     * @return string
     */
    public function credit_edit_list(){
        //接收参数
        $bank_id = IRequest::get('bank_id');
        $credit_product_id = IRequest::get('credit_product_id');
        $credit_product_name = IRequest::get('credit_product_name');
        $credit_status = IRequest::get('credit_status');
        $credit_fee = IRequest::get('credit_fee');
        $credit_level = IRequest::get('credit_level');
        $credit_currency = IRequest::get('credit_currency');
        $credit_agency = IRequest::get('credit_agency');
        $credit_privilege = IRequest::get('credit_privilege');
        $credit_purpose = IRequest::get('credit_purpose');
        $credit_color = IRequest::get('credit_color');
        $credit_type = IRequest::get('credit_type');

        //设置查询条件
        $credit_product_where['agent_id'] = get_login_agent_id();
        $credit_product_where['is_delete'] = IsDelete::No;
        $credit_product_where['credit_status'] = array("in",array(CreditStatus::edit,CreditStatus::audit,CreditStatus::audit_failure,CreditStatus::down));
        if(!empty($bank_id)) $credit_product_where['bank_id'] = $bank_id;
        if(!empty($credit_product_id)) $credit_product_where['credit_product_id'] = $credit_product_id;
        if(!empty($credit_product_name)) $credit_product_where['credit_product_name'] = array("like","%{$credit_product_name}%");
        if(!empty($credit_status)  && !in_array($credit_status,array(CreditStatus::release)))$credit_product_where['credit_status'] = $credit_status;
        if(!empty($credit_fee)) $credit_product_where['credit_fee'] = $credit_fee;
        if(!empty($credit_level)) $credit_product_where['credit_level'] = $credit_level;
        if(!empty($credit_currency)) $credit_product_where[''] =  array("exp","find_in_set({$credit_currency}, credit_currency)");
        if(!empty($credit_agency)) $credit_product_where['credit_agency'] = $credit_agency;
        if(!empty($credit_privilege)) $credit_product_where['credit_privilege'] = $credit_privilege;
        if(!empty($credit_purpose)) $credit_product_where['credit_purpose'] = $credit_purpose;
        if(!empty($credit_color)) $credit_product_where['credit_color'] = $credit_color;
        if(!empty($credit_type)) $credit_product_where['credit_type'] = $credit_type;

        //取得管理员详情
        $credit_list = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbOrder("credit_product_id desc")->pag();
        if ($credit_list === false){
            error('获取信用卡列表失败');
        }

        //取得银行数据
        $bank_list = IBankCache::getList();

        //页面赋值
        $this->assign("bank_id",$bank_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_product_name",$credit_product_name);
        $this->assign("credit_status",$credit_status);
        $this->assign("credit_fee",$credit_fee);
        $this->assign("credit_level",$credit_level);
        $this->assign("credit_currency",$credit_currency);
        $this->assign("credit_agency",$credit_agency);
        $this->assign("credit_privilege",$credit_privilege);
        $this->assign("credit_purpose",$credit_purpose);
        $this->assign("credit_color",$credit_color);
        $this->assign("credit_type",$credit_type);
        $this->assign("bank_list",$bank_list);
        $this->assign("credit_list",$credit_list);

        //渲染模板输出
        return view("credit_edit_list");
    }

    /**
     * @功能：已删除信用卡列表
     * @开发者：cxl
     * @return string
     */
    public function credit_del_list(){
        //接收参数
        $bank_id = IRequest::get('bank_id');
        $credit_product_id = IRequest::get('credit_product_id');
        $credit_bank_code = IRequest::get('credit_bank_code');
        $credit_product_name = IRequest::get('credit_product_name');
        $credit_status = IRequest::get('credit_status');
        $credit_fee = IRequest::get('credit_fee');
        $credit_level = IRequest::get('credit_level');
        $credit_currency = IRequest::get('credit_currency');
        $credit_agency = IRequest::get('credit_agency');
        $credit_privilege = IRequest::get('credit_privilege');
        $credit_purpose = IRequest::get('credit_purpose');
        $credit_color = IRequest::get('credit_color');
        $credit_type = IRequest::get('credit_type');

        //设置查询条件
        $credit_product_where['agent_id'] = get_login_agent_id();
        $credit_product_where['is_delete'] = IsDelete::Yes;
        if(!empty($bank_id)) $credit_product_where['bank_id'] = $bank_id;
        if(!empty($credit_product_id)) $credit_product_where['credit_product_id'] = $credit_product_id;
        if(!empty($credit_bank_code)) $credit_product_where['credit_bank_code'] = array("like","%{$credit_bank_code}%");
        if(!empty($credit_product_name)) $credit_product_where['credit_product_name'] = array("like","%{$credit_product_name}%");
        if(!empty($credit_status)) $credit_product_where['credit_status'] = $credit_status;
        if(!empty($credit_fee)) $credit_product_where['credit_fee'] = $credit_fee;
        if(!empty($credit_level)) $credit_product_where['credit_level'] = $credit_level;
        if(!empty($credit_currency)) $credit_product_where[''] =  array("exp","find_in_set({$credit_currency}, credit_currency)");
        if(!empty($credit_agency)) $credit_product_where['credit_agency'] = $credit_agency;
        if(!empty($credit_privilege)) $credit_product_where['credit_privilege'] = $credit_privilege;
        if(!empty($credit_purpose)) $credit_product_where['credit_purpose'] = $credit_purpose;
        if(!empty($credit_color)) $credit_product_where['credit_color'] = $credit_color;
        if(!empty($credit_type)) $credit_product_where['credit_type'] = $credit_type;

        //取得管理员详情
        $credit_list = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbOrder("credit_product_id desc")->pag();
        if ($credit_list === false){
            error('获取信用卡列表失败');
        }

        //取得银行数据
        $bank_list = IBankCache::getList();

        //页面赋值
        $this->assign("bank_id",$bank_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_bank_code",$credit_bank_code);
        $this->assign("credit_product_name",$credit_product_name);
        $this->assign("credit_status",$credit_status);
        $this->assign("credit_fee",$credit_fee);
        $this->assign("credit_level",$credit_level);
        $this->assign("credit_currency",$credit_currency);
        $this->assign("credit_agency",$credit_agency);
        $this->assign("credit_privilege",$credit_privilege);
        $this->assign("credit_purpose",$credit_purpose);
        $this->assign("credit_color",$credit_color);
        $this->assign("credit_type",$credit_type);
        $this->assign("bank_list",$bank_list);
        $this->assign("credit_list",$credit_list);

        //渲染模板输出
        return view("credit_del_list");
    }

    /**
     * @功能：信用卡详情
     * @开发者：cxl
     * @return string
     */
    public function credit_info(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得信用卡的银行分类列表
        $bank_type_list = IBankTypeCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getInfo(2);

        //取得信用卡的银行列表
        $bank_list = IBankCache::getList();

        //取得信用卡信息
        $credit_info = ICreditCache::getInfo($credit_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($credit_info)){

            if (!empty($credit_info['credit_content_img'])){
                $credit_info['credit_content_img'] = explode(",",$credit_info['credit_content_img']);
            }

            //整理币种
            $credit_info['credit_currency'] = explode(",",$credit_info['credit_currency']);

            //取得所在区域信息
            $credit_product_area = null;
            $credit_product_area['credit_product_id'] = $credit_product_id;
            $credit_product_area['province'] = array("exp","is not null");
            $credit_product_area['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('credit_product_area')->setDbFiled("province,city")->setDbWhere($credit_product_area)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_info",$credit_info);
        $this->assign("area_list",$area_list);
        $this->assign("bank_type_list",$bank_type_list);
        $this->assign("bank_list",$bank_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡日志
     * @开发者：cxl
     * @return string
     */
    public function credit_log(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得日志
        $credit_product_log_where['credit_product_id'] = $credit_product_id;
        $credit_product_log_list = IDb::getInstance('credit_product_log')->setDbWhere($credit_product_log_where)->setDbOrder("credit_product_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_product_log_list",$credit_product_log_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加信用卡
     * @开发者：cxl
     * @return string
     */
    public function credit_add(){

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得信用卡的银行列表
        $bank_list = IBankCache::getList();

        //页面赋值
        $this->assign("area_list",$area_list);
        $this->assign("bank_list",$bank_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加信用卡
     * @开发者：cxl
     * @return string
     */
    public function credit_edit(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得信用卡的银行列表
        $bank_list = IBankCache::getList();

        //取得信用卡信息
        $credit_info = ICreditCache::getInfo($credit_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($credit_info)){
            //整理数据
            $credit_info['credit_tag_ary'] = explode(",",$credit_info['credit_tag_str']);
            $credit_info['credit_content_img'] = explode(",",$credit_info['credit_content_img']);

            $credit_info['card_other'] = [];
            if(!empty($credit_info['credit_type_color'])){
                //整理数据
                $img_list = json_decode($credit_info['credit_img_list'],true);
                foreach ($img_list as $key => &$value) {
                    $value['type_name'] = HtmlEnumValue::$enum_value['credit_type'][$value['type']];
                    $value['color_name'] = HtmlEnumValue::$enum_value['credit_color'][$value['color']];
                }
                //其它卡面
                $credit_info['card_other'] = $img_list;
            }

            //整理币种
            $credit_info['credit_currency'] = explode(",",$credit_info['credit_currency']);

            //取得所在区域信息
            $credit_product_area_where = null;
            $credit_product_area_where['credit_product_id'] = $credit_product_id;
            $credit_product_area_where['province'] = array("exp","is not null");
            $credit_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('credit_product_area')->setDbFiled("province,city")->setDbWhere($credit_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_info",$credit_info);
        $this->assign("area_list",$area_list);
        $this->assign("bank_list",$bank_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加信用卡
     * @开发者：cxl
     * @return string
     */
    public function credit_add_submit(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id");
        $bank_id = IRequest::get("bank_id",IRequest::NOT_EMPTY,"银行编号不能为空！");
        $credit_product_name = IRequest::get("credit_product_name",IRequest::NOT_EMPTY,"信用卡名称不能为空！");
        $credit_product_url = IRequest::get("credit_product_url",IRequest::NOT_EMPTY,"信用卡图片地址不能为空！");
        $credit_product_outline = IRequest::get("credit_product_outline",IRequest::NOT_EMPTY,"信用卡概要说明不能为空！");
        $credit_fee = IRequest::get("credit_fee",IRequest::NOT_EMPTY,"信用卡年费不能为空！");
        $credit_level = IRequest::get("credit_level",IRequest::NOT_EMPTY,"信用卡等级不能为空！");
        $credit_currency = IRequest::get("credit_currency");
        $credit_agency = IRequest::get("credit_agency",IRequest::NOT_EMPTY,"信用卡发卡机构不能为空！");
        $credit_privilege = IRequest::get("credit_privilege",IRequest::NOT_EMPTY,"信用卡特权不能为空！");
        $credit_purpose = IRequest::get("credit_purpose",IRequest::NOT_EMPTY,"信用卡用途不能为空！");
        $credit_color = IRequest::get("credit_color",IRequest::NOT_EMPTY,"信用卡卡面颜色不能为空！");
        $credit_type = IRequest::get("credit_type",IRequest::NOT_EMPTY,"信用卡卡面类型不能为空！");
        $area = IRequest::get("area");
        $credit_content_txt = IRequest::get("credit_content_txt");
        $credit_content_img = IRequest::get("credit_content_img");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");
        $add_list = IRequest::get("add_list");

        //处理详情图片
        $credit_content_img = implode( ',', $credit_content_img);

        //处理自定义标签
        if (strlen($add_list)>=2){
            $add_list = substr($add_list,0,strlen($add_list)-1);
        }else{
            $add_list = null;
        }

        //判断标签是否合理
        if (!empty($add_list)){
            $add_ary = explode(",", $add_list);
            if (count($add_ary)>=6){
                //提示错误信息
                error("标签最多只能6个！");
            }

            foreach ($add_ary as $k=>$v){
                if(mb_strlen($v)<4||mb_strlen($v)>7){
                    //提示错误信息
                    error("每个标签最多只能4到7个字！");
                }
            }
        }

        //开启事物处理
        IDb::dbStartTrans();

        //判断是否为新添加数据
        if(empty($credit_product_id)) {
            //添加信用卡
            $credit_data = null;
            $credit_data['agent_id'] = get_login_agent_id();
            $credit_data['agent_admin_id'] = get_login_agent_admin_id();
            $credit_data['credit_product_name'] = $credit_product_name;
            $credit_data['credit_product_url'] = $credit_product_url;
            $credit_data['credit_product_outline'] = $credit_product_outline;
            $credit_data['credit_fee'] = $credit_fee;
            $credit_data['credit_level'] = $credit_level;
            $credit_data['credit_currency'] = join(",",$credit_currency);
            $credit_data['credit_agency'] = $credit_agency;
            $credit_data['credit_privilege'] = $credit_privilege;
            $credit_data['credit_purpose'] = $credit_purpose;
            $credit_data['credit_color'] = $credit_color;
            $credit_data['credit_type'] = $credit_type;
            $credit_data['credit_tag_str'] = $add_list;
            $credit_data['credit_status'] = CreditStatus::edit;
            $credit_data['bank_id'] = $bank_id;
            $credit_data['credit_content_txt'] = $credit_content_txt;
            $credit_data['credit_content_img'] = $credit_content_img;
            $credit_data['order_by'] = intval($order_by);
            $credit_data['remark'] = $remark;
            $credit_data['create_time'] = get_date_time();
            $credit_data['is_delete'] = IsDelete::No;
            $credit_add = IDb::getInstance('credit_product')->setDbData($credit_data)->add();
            if ($credit_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加信用卡基础信息失败，请联系管理员！");
            }

            //添加操作日志
            $credit_log_add = ILog::CreditLog($credit_add,null,get_login_agent_admin_id(),"创建信用卡！");
            if ($credit_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加信用卡基础信息失败，请联系管理员！");
            }

            //设置信用卡编号
            $credit_product_id = $credit_add;
        }else{
            //添加信用卡
            $credit_data = null;
            $credit_where = null;
            $credit_where['credit_product_id'] = $credit_product_id;
            $credit_data['credit_product_name'] = $credit_product_name;
            $credit_data['credit_product_url'] = $credit_product_url;
            $credit_data['credit_product_outline'] = $credit_product_outline;
            $credit_data['credit_fee'] = $credit_fee;
            $credit_data['credit_level'] = $credit_level;
            $credit_data['credit_currency'] = join(",",$credit_currency);
            $credit_data['credit_agency'] = $credit_agency;
            $credit_data['credit_privilege'] = $credit_privilege;
            $credit_data['credit_purpose'] = $credit_purpose;
            $credit_data['credit_color'] = $credit_color;
            $credit_data['credit_type'] = $credit_type;
            $credit_data['credit_tag_str'] = $add_list;
            $credit_data['credit_status'] = CreditStatus::edit;
            $credit_data['bank_id'] = $bank_id;
            $credit_data['credit_content_txt'] = $credit_content_txt;
            $credit_data['credit_content_img'] = $credit_content_img;
            $credit_data['order_by'] = intval($order_by);
            $credit_data['remark'] = $remark;
            $credit_data['update_time'] = get_date_time();
            $credit_data['is_delete'] = IsDelete::No;
            $credit_upd = IDb::getInstance('credit_product')->setDbData($credit_data)->setDbWhere($credit_where)->upd();
            if ($credit_upd === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加信用卡基础信息失败，请联系管理员！");
            }

            //添加操作日志
            $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"修改信用卡！");
            if ($credit_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加信用卡基础信息失败，请联系管理员！");
            }
        }

        //删除历史效区域数据
        $credit_area_where['credit_product_id'] = $credit_product_id;
        $credit_area_del = IDb::getInstance('credit_product_area')->setDbWhere($credit_area_where)->del();
        if ($credit_area_del === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("设置信用卡有效区域失败！");
        }

        //判断信用卡有效区域
        if(is_array($area) && count($area) > 0){
            //循环写数据
            foreach($area as $item){
                //拆分省份及城市
                list($province,$city) = explode(",",$item);
                if(empty($province) || empty($city)){
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("设置信用卡有效区域失败！");
                }

                //写有效区域数据
                $credit_area_data = null;
                $credit_area_data['credit_product_id'] = $credit_product_id;
                $credit_area_data['province'] = $province;
                $credit_area_data['city'] = $city;
                $credit_area_data['create_time'] = get_date_time();
                $credit_area_add = IDb::getInstance('credit_product_area')->setDbData($credit_area_data)->add();
                if ($credit_area_add === false) {
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("设置信用卡有效区域失败！");
                }
            }
        }else{
            //写有效区域数据
            $credit_area_data = null;
            $credit_area_data['credit_product_id'] = $credit_product_id;
            $credit_area_data['province'] = null;
            $credit_area_data['city'] = null;
            $credit_area_data['create_time'] = get_date_time();
            $credit_area_add = IDb::getInstance('credit_product_area')->setDbData($credit_area_data)->add();
            if ($credit_area_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("设置信用卡有效区域失败！");
            }
        }

        //提交事物处理
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("credit_edit_list"));
    }

    /**
     * @功能：提交审核
     * @开发者：cxl
     * @return string
     */
    public function credit_commit(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //开启事物
        IDb::dbStartTrans();

        //修改管理员信息
        $credit_product_where['credit_product_id'] = $credit_product_id;
        $credit_product_data['credit_status'] = CreditStatus::audit;
        $credit_product_data['update_time'] = get_date_time();
        $credit_upd = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbData($credit_product_data)->upd();
        if($credit_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡提交审核失败！");
        }

        //添加操作日志
        $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"信用卡提交审核！");
        if ($credit_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡提交审核失败！");
        }

        //提交事物处理
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信用卡审核
     * @开发者：cxl
     * @return string
     */
    public function credit_audit(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得信用卡的银行分类列表
        $bank_type_list = IBankTypeCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得信用卡的银行列表
        $bank_list = IBankCache::getList();

        //取得信用卡信息
        $credit_info = ICreditCache::getInfo($credit_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($credit_info)){
            //整理数据
            $credit_info['credit_condition_str'] = json_decode($credit_info['credit_condition_str'],true);
            $credit_info['credit_privilege_str'] = json_decode($credit_info['credit_privilege_str'],true);
            $credit_info['credit_base_str'] = json_decode($credit_info['credit_base_str'],true);
            $credit_info['credit_fee_str'] = json_decode($credit_info['credit_fee_str'],true);
            $credit_info['credit_discount_str'] = json_decode($credit_info['credit_discount_str'],true);
            $credit_info['credit_cheat_str'] = json_decode($credit_info['credit_cheat_str'],true);
            $credit_info['credit_interest_str'] = json_decode($credit_info['credit_interest_str'],true);

            //整理币种
            $credit_info['credit_currency'] = explode(",",$credit_info['credit_currency']);

            //取得所在区域信息
            $credit_product_area_where['credit_product_id'] = $credit_product_id;
            $credit_product_area_where['province'] = array("exp","is not null");
            $credit_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('credit_product_area')->setDbFiled("province,city")->setDbWhere($credit_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_info",$credit_info);
        $this->assign("area_list",$area_list);
        $this->assign("bank_type_list",$bank_type_list);
        $this->assign("bank_list",$bank_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡审核提交
     * @开发者：cxl
     * @return string
     */
    public function credit_audit_submit(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");
        $credit_status = IRequest::get("credit_status",IRequest::NOT_EMPTY,"信用卡编号不能为空！");
        $audit_note = IRequest::get("audit_note");

        //开启事物
        IDb::dbStartTrans();

        //修改管理员信息
        $credit_product_where['credit_product_id'] = $credit_product_id;
        $credit_product_data['credit_status'] = $credit_status;
        $credit_product_data['audit_note'] = $audit_note;
        $credit_product_data['update_time'] = get_date_time();
        $credit_upd = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbData($credit_product_data)->upd();
        if($credit_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡审核错误！");
        }

        //添加操作日志
        $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"信用卡审核！");
        if ($credit_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡审核错误！");
        }

        //提交事物处理
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("credit_edit_list"));
    }

    /**
     * @功能：信用卡下架
     * @开发者：cxl
     * @return string
     */
    public function credit_down(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $credit_product_where['credit_product_id'] = $credit_product_id;
        $credit_product_data['credit_status'] = CreditStatus::down;
        $credit_product_data['update_time'] = get_date_time();
        $credit_upd = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbData($credit_product_data)->upd();
        if($credit_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("信用卡下架失败！");
        }

        //修改管理员信息
        $credit_recommend_where['credit_product_id'] = $credit_product_id;
        $credit_recommend_data['is_delete'] = IsDelete::Yes;
        $credit_recommend_data['update_admin'] = get_login_agent_admin_id();
        $credit_recommend_data['update_time'] = get_date_time();
        $credit_recommend_upd = IDb::getInstance('credit_recommend')->setDbWhere($credit_recommend_where)->setDbData($credit_recommend_data)->upd();
        if($credit_recommend_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("信用卡删除失败！");
        }

        //添加操作日志
        $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"信用卡下架！");
        if ($credit_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡下架失败！");
        }

        //事物提交
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信用卡删除
     * @开发者：cxl
     * @return string
     */
    public function credit_del(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $credit_product_where['credit_product_id'] = $credit_product_id;
        $credit_product_data['is_delete'] = IsDelete::Yes;
        $credit_product_data['update_time'] = get_date_time();
        $credit_upd = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbData($credit_product_data)->upd();
        if($credit_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("信用卡删除失败！");
        }

        //修改管理员信息
        $credit_recommend_where['credit_product_id'] = $credit_product_id;
        $credit_recommend_data['is_delete'] = IsDelete::Yes;
        $credit_recommend_data['update_admin'] = get_login_agent_admin_id();
        $credit_recommend_data['update_time'] = get_date_time();
        $credit_recommend_upd = IDb::getInstance('credit_recommend')->setDbWhere($credit_recommend_where)->setDbData($credit_recommend_data)->upd();
        if($credit_recommend_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("信用卡推荐删除失败！");
        }

        //添加操作日志
        $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"信用卡删除！");
        if ($credit_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡删除失败！");
        }

        //事物提交
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信用卡还原
     * @开发者：cxl
     * @return string
     */
    public function credit_recycle(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $credit_product_where['credit_product_id'] = $credit_product_id;
        $credit_product_data['is_delete'] = IsDelete::No;
        $credit_product_data['update_time'] = get_date_time();
        $credit_upd = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->setDbData($credit_product_data)->upd();
        if($credit_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡还原失败！");
        }

        //添加操作日志
        $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"信用卡还原！");
        if ($credit_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信用卡还原失败！");
        }

        //事物提交
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信用卡编辑标签
     * @开发者：cxl
     * @return string
     */
    public function credit_tag(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得标签列表
        $credit_tag_list = ICreditTagCache::getList();
        if($credit_tag_list === false){
            error("取得标签列表失败！");
        }

        //取得标签
        $credit_product_info = ICreditCache::getInfo($credit_product_id);
        if($credit_product_info === false){
            error("取得标签列表失败！");
        }

        //整理标签信息
        $credit_tag = array();
        if(!empty($credit_product_info['credit_tag_str'])){
            $credit_tag = explode(",",$credit_product_info['credit_tag_str']);
        }

        //整理自定义标签
        if (!empty($credit_product_info['credit_ext_tag_str'])){
            $credit_product_info['credit_ext_tag_ary'] = explode(",",$credit_product_info['credit_ext_tag_str']);
        }

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_product_info",$credit_product_info);
        $this->assign("credit_tag_list",$credit_tag_list);
        $this->assign("credit_tag",$credit_tag);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡编辑标签
     * @开发者：cxl
     * @return string
     */
    public function credit_tag_submit(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");
        $credit_tag_array = IRequest::get("credit_tag_id");
        $add_list = IRequest::get("add_list");

        //处理自定义标签
        if (strlen($add_list)>=2){
            $add_list = substr($add_list,0,strlen($add_list)-1);
        }else{
            $add_list = null;
        }

        //判断是是否为数组
        if(!is_array($credit_tag_array) || empty($credit_tag_array)){
            error("请选择信用卡标签！");
        }

        //开启事物出来
        IDb::dbStartTrans();

        //添加新标签列表
        $credit_product_where['credit_product_id'] = $credit_product_id;
        $credit_product_data['credit_tag_str'] = join(",",$credit_tag_array);
        $credit_product_data['credit_ext_tag_str'] = $add_list;
        $credit_product_data['update_time'] = get_date_time();
        $credit_product_upd = IDb::getInstance('credit_product')->setDbData($credit_product_data)->setDbWhere($credit_product_where)->upd();
        if($credit_product_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保存信用卡标签失败！");
        }

        //添加操作日志
        $credit_log_add = ILog::CreditLog($credit_product_id,null,get_login_agent_admin_id(),"修改信用卡标签！");
        if ($credit_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("修改信用卡标签失败！");
        }

        //事物提交
        IDb::dbCommit();

        //删除缓存信息
        ICreditCache::deleteInfo($credit_product_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("credit_edit_list"));
    }

    /**
     * @功能：信用卡分享记录
     * @开发者：cxl
     * @return string
     */
    public function credit_share_log(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $credit_product_id = IRequest::get("credit_product_id");

        //取得数据
        $credit_share_where = null;
        $credit_share_where['cp.agent_id'] = get_login_agent_id();
        if(!empty($user_id)) $credit_share_where['cs.user_id'] = $user_id;
        if(!empty($credit_product_id)) $credit_share_where['cs.credit_product_id'] = $credit_product_id;
        $credit_share_list = IDb::getInstance("credit_share as cs")
            ->setDbFiled('cs.*')
            ->setDbJoin('credit_product as cp', 'cp.credit_product_id = cs.credit_product_id')
            ->setDbWhere($credit_share_where)
            ->setDbOrder('cs.share_id desc')
            ->pag();
        if ($credit_share_list === false){
            error('获取信用卡分享记录失败');
        }

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_share_list",$credit_share_list);

        //渲染模板输出
        return view();
    }
}
